﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Validation;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Coslen.TahorCRM.Domain.Entities.ViewModels.Editing;
using Coslen.TahorCRM.Domain.Entities.ViewModels.Searching;
using Coslen.TahorCRM.Domain.Services.Interfaces;
using Coslen.TahorCRM.UI.www.Controllers;
using Ninject;

namespace Coslen.TahorCRM.UI.www.Areas.Admin.Controllers
{
    public class ContactEventTypeController : TahorBaseController
    {

        [Inject]
        public IContactEventTypeService ContactEventTypeService { get; set; }

        //
        // GET: /Admin/ContactEventType/

        public ActionResult Index(ContactEventTypeSearchViewModel model)
        {
            try
            {
                if (model == null)
                {
                    model = new ContactEventTypeSearchViewModel();
                    // any defaults???
                }

                model = ContactEventTypeService.GetContactEventTypeSearchViewModel(model);
                return View(model);
            }
            catch (DbEntityValidationException dbEx)
            {
                ProcessDbEntityValidationException(dbEx);
            }
            catch (DbUpdateException dbEx)
            {
                ProcessDbUpdateException(dbEx);
            }
            catch (SqlException sqlEx)
            {
                ProcessSqlException(sqlEx);
            }

            throw new ApplicationException("Should  not be able to get here!");
        }

        //
        // GET: /Admin/ContactEventType/Details/5

        public ActionResult Details(int id)
        {
            try
            {
                ContactEventTypeEditViewModel model = ContactEventTypeService.GetContactEventTypeEditViewModelById(id);
                return View(model);
            }
            catch (DbEntityValidationException dbEx)
            {
                ProcessDbEntityValidationException(dbEx);
            }
            catch (DbUpdateException dbEx)
            {
                ProcessDbUpdateException(dbEx);
            }
            catch (SqlException sqlEx)
            {
                ProcessSqlException(sqlEx);
            }

            throw new ApplicationException("Should  not be able to get here!");
        }

        //
        // GET: /Admin/ContactEventType/Create

        public ActionResult Create()
        {
            try
            {
                ContactEventTypeEditViewModel model = ContactEventTypeService.GetNewContactEventTypeEditViewModel();
                return View(model);
            }
            catch (DbEntityValidationException dbEx)
            {
                ProcessDbEntityValidationException(dbEx);
            }
            catch (DbUpdateException dbEx)
            {
                ProcessDbUpdateException(dbEx);
            }
            catch (SqlException sqlEx)
            {
                ProcessSqlException(sqlEx);
            }

            throw new ApplicationException("Should  not be able to get here!");
        }

        //
        // POST: /Admin/ContactEventType/Create

        [HttpPost]
        public ActionResult Create(ContactEventTypeEditViewModel model)
        {
            try
            {
                // TODO: Add insert logic here
                if (ModelState.IsValid)
                {
                    ContactEventTypeService.SaveContactEventType(model.ContactEventType);
                    return RedirectToAction("Index");
                }
                else
                {
                    ProcessModelState(ModelState);
                }
            }
            catch (DbEntityValidationException dbEx)
            {
                ProcessDbEntityValidationException(dbEx);
            }
            catch (DbUpdateException dbEx)
            {
                ProcessDbUpdateException(dbEx);
            }
            catch (SqlException sqlEx)
            {
                ProcessSqlException(sqlEx);
            }

            return View(model);
        }

        //
        // GET: /Admin/ContactEventType/Edit/5

        public ActionResult Edit(int id)
        {
            try
            {
                ContactEventTypeEditViewModel model = ContactEventTypeService.GetContactEventTypeEditViewModelById(id);
                return View(model);
            }
            catch (DbEntityValidationException dbEx)
            {
                ProcessDbEntityValidationException(dbEx);
            }
            catch (DbUpdateException dbEx)
            {
                ProcessDbUpdateException(dbEx);
            }
            catch (SqlException sqlEx)
            {
                ProcessSqlException(sqlEx);
            }

            throw new ApplicationException("Should  not be able to get here!");
        }

        //
        // POST: /Admin/ContactEventType/Edit/5

        [HttpPost]
        public ActionResult Edit(ContactEventTypeEditViewModel model)
        {
            try
            {
                // TODO: Add update logic here
                if (ModelState.IsValid)
                {
                    ContactEventTypeService.SaveContactEventTypeEditViewModel(model);
                    return RedirectToAction("Index");
                }
                else
                {
                    ProcessModelState(ModelState);
                }

                return RedirectToAction("Index");
            }
            catch (DbEntityValidationException dbEx)
            {
                ProcessDbEntityValidationException(dbEx);
            }
            catch (DbUpdateException dbEx)
            {
                ProcessDbUpdateException(dbEx);
            }
            catch (SqlException sqlEx)
            {
                ProcessSqlException(sqlEx);
            }

            // TODO: add any alter failed edit model logic here....
            ContactEventTypeEditViewModel tmpModel = ContactEventTypeService.GetContactEventTypeEditViewModelById(model.ContactEventType.ContactEventTypeId);
            model.PossibleSelectedRoles = tmpModel.PossibleSelectedRoles;

            return View(model);
        }

        //
        // GET: /Admin/ContactEventType/Delete/5

        public ActionResult Delete(int id)
        {
            try
            {
                ContactEventTypeEditViewModel model = ContactEventTypeService.GetContactEventTypeEditViewModelById(id);
                return View(model);
            }
            catch (DbEntityValidationException dbEx)
            {
                ProcessDbEntityValidationException(dbEx);
            }
            catch (DbUpdateException dbEx)
            {
                ProcessDbUpdateException(dbEx);
            }
            catch (SqlException sqlEx)
            {
                ProcessSqlException(sqlEx);
            }

            throw new ApplicationException("Should  not be able to get here!");
        }

        //
        // POST: /Admin/ContactEventType/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirm(int id)
        {
            try
            {
                // TODO: Add delete logic here
                ContactEventTypeEditViewModel model = ContactEventTypeService.GetContactEventTypeEditViewModelById(id);
                ContactEventTypeService.DeleteContactEventType(model.ContactEventType);
                return RedirectToAction("Index");
            }
            catch (DbEntityValidationException dbEx)
            {
                ProcessDbEntityValidationException(dbEx);
            }
            catch (DbUpdateException dbEx)
            {
                ProcessDbUpdateException(dbEx);
            }
            catch (SqlException sqlEx)
            {
                ProcessSqlException(sqlEx);
            }

            return RedirectToAction("Delete", new { id = id });
        }
    }
}
